package com.anyou.mall.dri.service.impl;

import com.anyou.mall.dri.domain.Notice;
import com.anyou.mall.dri.mapper.NoticeMapper;
import com.anyou.mall.dri.service.INoticeService;
import com.anyou.mall.order.domain.OrdOrder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liqipeng
 * @description 司机通知-业务实现类
 * @date 2022-04-02
 */
@Slf4j
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;


    @Override
    public boolean insertDataByPlatformNotice(Long platformNoticeId, List<Long> userIds) {
        List<Notice> noticeList = userIds.stream().map(driverId -> {
            Notice notice = new Notice();
            notice.setDriverId(driverId);
            notice.setJumpType(0);
            notice.setPlatformNoticeId(platformNoticeId);
            notice.setNoticeType("2");
            return notice;
        }).collect(Collectors.toList());
        boolean b = this.saveBatch(noticeList);
        return b;
    }

    @Override
    public void insertNotice(OrdOrder ordOrder, int type) {
        Notice notice = new Notice();
        notice.setNoticeType("3");
        notice.setCreateTime(new Date());
        if(type==2){//配送员
            notice.setDriverId(ordOrder.getDeliveryUserId());
        }
        if(type==1){
            notice.setDriverId(ordOrder.getCollectUserId());
        }
        notice.setContent("系统给你派了新的订单"+ordOrder.getOrderNo()+",请查看");
        notice.setOrderId(ordOrder.getOrderId());
        notice.setOrderNo(ordOrder.getOrderNo());
        notice.setJumpType(1);
        this.save(notice);

    }

    @Override
    public boolean deleteDataByPlatformNotice(Long platformNoticeId) {
        boolean remove = this.remove(new QueryWrapper<Notice>().lambda()
                .eq(Notice::getPlatformNoticeId, platformNoticeId));
        return remove;
    }
}
